<?php
namespace Api\Controller;
class HerdController extends ApiController{

    public function __construct(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
    }
    
    //判断群名称是否已经被占用
    function yanzheng(){
        params_exist(array('title'));
        $count = M('herd')->where(array('title'=>$title))->count();
        if ($count > 0) {
            response(1,array('is_use'=>true));
        }else{
            response(1,array('is_use'=>false));
        }
    }
    //自选号
    // function zixuan(){
    //     $title = $_POST['title'];
    //     $sql = 'select id from '.$this->table.' where title="'.$title.'" union select id from '.$this->herd_title.' where title=title="'.$title.'"';
    //     $r = $this->db->getNum($sql);
    //     if($r){
    //         $this->error();
    //     }else{
    //         $this->success(array('title'=>$title));
    //     }
    // }
    //团名称列表
    // function title(){
    //     $sql = 'select * from '.$this->herd_title.' where type=0 order by rand() limit 12';
    //     $list = $this->db->getAll($sql);
    //     $this->success(array('list'=>$list));
    // }
    //购买团号
    // function buy_title(){
    //     $id = $_POST['id']+0;
    //     $uid = $_POST['uid']+0;
    //     $hid = $_POST['hid']+0;
    //     $price = $_POST['price']+0;
    //     $title = $_POST['title'];
    //     $this->yuer($uid,$price);
    //     mysql_query('begin');//开始事务
    //     $sql = 'update '.$this->herd_title.' set type=1,u_id='.$uid.' where id='.$id;
    //     $r = $this->db->query($sql);
    //     //减去余额
    //     $sql = 'update '.$this->user.' set balance=balance-'.$price.' where id='.$uid;
    //     $r1 = $this->db->query($sql);
    //     if($r && $r1){
    //         mysql_query('commit');
    //         //设置群号
    //         $sql = 'update '.$this->herd.' set title="'.$title.'" where id='.$hid;
    //         $r1 = $this->db->query($sql);
    //         $this->success();
    //     }else{
    //         mysql_query('rollback');
    //         $this->error();
    //     }
    // }
    //判断是否已创建四个群
    function siqun(){
        $count = M('herd')->where(array('type'=>1,'creater'=>API_UID))->count();
        if ($count > 3) {
            response(-2,array('count'=>$count),"已创建过四个群");
        }else{
            response(1,array('count'=>$count));
        }
    }
    //创建群组
    function create(){
        $uid = API_UID;
        $_POST['creater'] = $uid;
        $_POST['register'] = time();
        $data = $this->verify();
        //判断是滞已有创建过群
        $sql = 'select id from '.$this->table.' where label=1 and creater='.$uid;
        $r = $this->db->getNum($sql);
        //如果是购买自选号的话
        if(!empty($_POST['prite'])){
            $this->yuer($uid,$_POST['prite']);
            mysql_query('begin');//开始事务
            //减去余额
            $sql = 'update '.$this->user.' set balance=balance-'.$_POST['prite'].' where id='.$uid;
            $r1 = $this->db->query($sql);
        }elseif(!empty($data['title'])){
            $this->siqun($uid);
            $data['title'] = $data['title'];
        }elseif($r){
            //已经创建过手机号团了
            $this->siqun($uid);
            $title = $this->yanzheng();
            $data['title'] = $title;
        }else{
            $data['label'] = 1;
            $data['title'] = $_POST['phone'];
        }
        $data['litpic'] = empty($_FILES['litpic']['name'])?'':Upload::up('litpic');
        $r = $this->db->insert($this->table,$data);
        if($r){
            $id = mysql_insert_id();
            //添加群主
            $sql = 'insert into '.$this->member." (h_id,u_id,purviews) values ($id,$uid,1)";
            $r2 = $this->db->query($sql);
            if(!empty($_POST['prite'])){
                mysql_query('commit');
            }
            $info = $this->db->getField($this->table,$id);
            //添加用户环信id
            $info['phone'] = $_POST['phone'];
            $this->success($info);
        }else{
            if(!empty($_POST['prite'])){
                mysql_query('rollback');
            }
            $array = array('err_code'=>'创建失败');
            $this->error($array);
        }
    }
    //修改群组
    function update(){
        //去除不可修改项
        unset($_POST['type']);
        unset($_POST['creater']);
        unset($_POST['register']);
        $r = $this->db->update($this->table,$_POST);
        if($r){
            $sql = 'select * from '.$this->table.' where id='.$_POST['id'];
            $info = $this->db->getRow($sql);
            $this->success($info);
        }else{
            $array = array('err_code'=>'修改失败');
            $this->error($array);
        }
    }
    //群详情
    function info(){
        $id = $_POST['id'];
        //群详情
        $sql = 'select h.*,u.phone from '.$this->table.' as h left join '.$this->user.' as u on h.creater=u.id where h.id='.$id;
        $info = $this->db->getRow($sql);
        //群分队
        $sql = 'select * from '.$this->element.' where type=1 and u_id='.$id;
        $r = $this->db->getAll($sql);
        foreach($r as $v){
            $info['fendui'][$v['e_id']] = $v;
        }
        //群成员
        $sql = 'select m.*,u.phone,u.name,u.litpic from '.$this->member.' as m left join '.$this->user.' as u on m.u_id=u.id where h_id='.$id;
        $r = $this->db->getAll($sql);
        foreach($r as $v){
            $info['fendui'][$v['e_id']]['child'][] = $v;
        }
        sort($info['fendui']);
        $this->success($info);
    }
    //邀请or申请进群一级群
    function access(){
        $data = array('h_id'=>$_POST['id'],'u_id'=>$_POST['u_id']);
        //判断加入的群是否是手机群
        $sql = 'select id from '.$this->table.' where label=1 and id='.$_POST['id'];
        if($this->db->getNum($sql)){
            $sql = 'select id from '.$this->member.' as m left join '.$this->table.' as h on m.h_id=h.id where label=1 and purviews=0 and u_id='.$_POST['u_id'];
            $r = $this->db->getNum($sql);
            if($r){
                $array = array('err_code'=>'只可以加入一个手机团');
                $this->error($array);
            }
        }
        $sql = 'insert into '.$this->member.' (h_id,u_id) values ('.$data['h_id'].','.$data['u_id'].')';
        $r = $this->db->query($sql);
        if($r){
            //环信群成员添加
            $hx = new Hxcall();
            $array = array($_POST['username']);
            $hx = $hx->hx_Addgroupmembers($_POST['group_id'],$array);
            $hx = json_decode($hx);
            if(!empty($hx->error)){
                $sql = 'delete from '.$this->member.' where h_id='.$data['h_id'].' and u_id='.$data['u_id'];
                $this->db->query($sql);
                $array = array('err_code'=>$hx->error);
                $this->error($array);
            }
            $this->success(array());
        }else{
            $this->error(array());
        }
    }
    //进入二级群
    function secondaryAdd(){
        $hid = $_POST['hid'];
        $eid = $_POST['eid'];
        $uid = $_POST['uid'];
        //环信
        $hx_hid = $_POST['hx_hid'];
        $hx_uid = $_POST['hx_uid'];
        $sql = 'update '.$this->member." set e_id=$eid where h_id=$hid and u_id in ($uid)";
        $r = $this->db->query($sql);
        //回滚
        $sql = 'update '.$this->member." set e_id=0 where h_id=$hid and u_id in ($uid)";
        if(!$r){
            $this->db->query($sql);
            $this->error(array('err_code'=>'mysql出错'));
        }
        $hx = new Hxcall();
        $hx = $hx->hx_Addgroupmembers($hx_hid,$hx_uid);
        $hx = json_decode($hx);
        if(!empty($hx->error)){
            $this->db->query($sql);
            $array = array('err_code'=>$hx->error);
            $this->error($array);
        }
        $this->success(array());
    }
    //删除or退出群
    function quit(){
        $sql = 'select * from '.$this->member.' as m left join '.$this->element.' as e on m.e_id=e.e_id where h_id='.$_POST['h_id'].' and m.u_id='.$_POST['u_id'];
        $r = $this->db->getRow($sql);
        //如果有二级群的话
        if(!empty($r['e_id'])){
            //如果是二群群主的话
            if($r['purviews']==2){
                //删除分队
                $sql = 'delete from '.$this->element.' where e_id='.$r['e_id'];
                $this->db->query($sql);
                //群成员退出二级群
                $sql = 'update '.$this->member.' set e_id=0 where e_id='.$r['e_id'];
                $this->db->query($sql);
                $hx = new Hxcall();
                $hx = $hx->hx_Deletechatgroups($r['hx_id']);
                $hx = json_decode($hx);
                if(!empty($hx->error)){
                    $array = array('err_code'=>$hx->error);
                    $this->error($array);
                }
            }else{
                $hx = new Hxcall();
                $hx = $hx->hx_Deletegroupmembers($r['e_id'],$_POST['username']);
            }
        }
        $sql = 'delete from '.$this->member.' where h_id='.$_POST['h_id'].' and u_id='.$_POST['u_id'];
        $r = $this->db->query($sql);
        if($r){
            //环信群成员删除
            $hx = new Hxcall();
            $hx = $hx->hx_Deletegroupmembers($_POST['group_id'],$_POST['username']);
            $this->success(array());
        }else{
            $this->error(array());
        }
    }
    //群成员名称备注
    function remark(){
        $uid    = $_POST['u_id'];
        $hid    = $_POST['h_id'];
        $name   = $_POST['name'];
        $sql = 'update '.$this->member." set remarkName='$name' where u_id=$uid and h_id=$hid";
        $r = $this->db->query($sql);
        if($r){
            $this->success(array());
        }else{
            $this->error(array());
        }
    }
    //删除二级群成员
    function secondaryQuit(){
        $hid = $_POST['hid'];
        $eid = $_POST['eid'];
        $uid = $_POST['uid'];
        //环信
        $hx_hid = $_POST['hx_hid'];
        $hx_uid = $_POST['hx_uid'];
        $sql = 'update '.$this->member." set e_id=0 where h_id=$hid and u_id in ($uid)";
        $r = $this->db->query($sql);
        //回滚
        $sql = 'update '.$this->member." set e_id=$eid where h_id=$hid and u_id in ($uid)";
        if(!$r){
            $this->db->query($sql);
            $this->error(array('err_code'=>'mysql出错'));
        }
        $hx = new Hxcall();
        $hx = $hx->hx_Deletegroupmembers($hx_hid,$hx_uid);
        $hx = json_decode($hx);
        if(!empty($hx->error)){
            $this->db->query($sql);
            $array = array('err_code'=>$hx->error);
            $this->error($array);
        }
        $this->success(array());
    }
    //排行榜
    function ranking(){
        $sql = 'select h_id,count(u_id) as count,h.*  from '.$this->member.' as m left join '.$this->table.' as h on m.h_id=h.id where is_close=0 group by h_id order by count desc';
        $list = $this->db->getAll($sql);
        if($list){
            $this->success(array('list'=>$list));
        }else{
            $this->error(array());
        }
    }
}
?>